package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.SystemDictionary;
import com.xmg.p2p.base.domain.SystemDictionaryItem;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.SystemDictionaryItemMapper;
import com.xmg.p2p.base.mapper.SystemDictionaryMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.SystemDictionaryQueryObject;
import com.xmg.p2p.base.service.SystemDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryServiceImpl implements SystemDictionaryService {

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;
    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    /**
     * 数据字典分类查询
     */
    public PageResult queryDics(SystemDictionaryQueryObject qo) {
        // 查看是否有此字典的信息。
        int count  = this.systemDictionaryMapper.queryForCount(qo);
        if (count > 0) {    // 大于零代表有此信息
            // 把此字典的信息全部查询出保存到List集合中...
            List<SystemDictionary> list = this.systemDictionaryMapper.query(qo);
            /**
             * PageResult(list, count, qo.getCurrentPage(), qo.getPageSize()
             * list：通过关键词查询到所有有关联的数据列表
             * count：通过关键词查询到所有有关联的数据个数
             * qo.getCurrentPage()  起始页
             * qo.getPageSize()     每页显示的行数
             */
            return new PageResult(list, count, qo.getCurrentPage(), qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    /*
     * 数据字典的新增和更改
     */
    public void saveOrUpdateDic(SystemDictionary sd) {
        //ID是自动生成，所以我们可以根据id是否存在来完成是添加还是修改的操作判断
        if (sd.getId() != null) {
            this.systemDictionaryMapper.updateByPrimaryKey(sd);//表示更新操作
        }else{
            this.systemDictionaryMapper.insert(sd) ; //表示新增操作
        }
    }

    /**
     * 数据明细的分页查询
     * @param qo
     * @return
     */
    public PageResult queryDicsItems(SystemDictionaryQueryObject qo) {
        int count = this.systemDictionaryItemMapper.queryForCount(qo);
        if (count > 0){
            List<SystemDictionaryItem> query = this.systemDictionaryItemMapper.query(qo);
            return new PageResult(query,count,qo.getCurrentPage(),qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    public List<SystemDictionary> listDics() {
        // 查询数据字典的分类
        return this.systemDictionaryMapper.selectAll();
    }

    @Override
    public void saveOrUpdateDicItem(SystemDictionaryItem sd) {
        if (sd.getId() != null){
            this.systemDictionaryItemMapper.updateByPrimaryKey(sd); // 表示更新操作
        }else {
            this.systemDictionaryItemMapper.insert(sd);     // 表示添加操作
        }
    }

    @Override
    public List<SystemDictionaryItem> selectByParentSn(String sn) {
        List<SystemDictionaryItem> systemDictionaryItems = this.systemDictionaryItemMapper.selectByParentSn(sn);
        return systemDictionaryItems;
    }
}
